//
//  LogisticsInfoViewController.m
//  OMS
//
//  Created by goldeneye on 14/11/4.
//  Copyright (c) 2014年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "MTCRWDLogisticsInfoViewController.h"
#import "MTCRWDLogisticsInfoViewCell.h"

#import "MJRefresh.h"
#import "UITableView+Custom.h"
#import "NSDictionary+DataFormat.h"
#import "WebServiceCommonRequest.h"

@interface MTCRWDLogisticsInfoViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) NSMutableArray *logisticsData;
@property (nonatomic,strong) NSMutableDictionary *orderBaseInfo;

@end

@implementation MTCRWDLogisticsInfoViewController
@synthesize logisticsData;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setTitle: @"发货单明细"];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.logisticsData = [NSMutableArray array];
    
    // Do any additional setup after loading the view from its nib.
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.tableView registerClass:[MTCRWDLogisticsInfoViewCell class] forCellReuseIdentifier:@"MTCLogisticsInfoViewCell"];
    
    NSString *DHDBH = [self.orderBaseInfo objectFormatForKey:@"任务单编号"];
    NSString *SGBW = [self.orderBaseInfo objectFormatForKey:@"施工部位"];
    NSString *JHFL = [self.orderBaseInfo objectFormatForKey:@"计划方量"];
    self.DHDBHLable.text = DHDBH;
    self.SGBWLable.text = SGBW;
    self.JHFLLable.text = [NSString stringWithFormat:@"%@方",JHFL];
    
    NSString *FCCS = [self.orderBaseInfo objectFormatForKey:@"发车次数"];
    NSString *LJFHFL = [self.orderBaseInfo objectFormatForKey:@"发货方量"];
    self.logisticsaLable.text = [NSString stringWithFormat:@"已发出%@车，累计发货%@方",FCCS,LJFHFL];
    
    //获取数据
    [self.tableView showTableViewProgressOffset:30];
    [self headerRereshing];
}

- (void)setupRefresh
{
    //添加下拉加载数据
    //[self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    
    //[self.tableView headerBeginRefreshing];
    //[self.tableView footerEndRefreshing];
}

- (void)stopRefresh
{
    //移除下拉加载数据
    [self.tableView headerEndRefreshing];
    [self.tableView footerEndRefreshing];
}

#pragma mark 开始进入刷新状态

- (void)headerRereshing
{
    NSNumber *stateNumber = [NSNumber numberWithInteger:0];
    NSNumber *endNumber = [NSNumber numberWithInteger:10];
    [self getNetWorkDataIsReresh:YES state:stateNumber end:endNumber];
}

- (void)footerRereshing
{
    NSNumber *stateNumber = [NSNumber numberWithInteger:self.logisticsData.count + 1];
    NSNumber *endNumber = [NSNumber numberWithInteger:self.logisticsData.count + 10];
    [self getNetWorkDataIsReresh:NO state:stateNumber end:endNumber];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getPassingParameter:(id)object
{
    NSMutableDictionary *dic = object;
    self.orderBaseInfo = dic;
}

#pragma mark 网络获取数据

- (void)getNetWorkDataIsReresh:(BOOL)isReresh state:(NSNumber*)state end:(NSNumber*)end
{
    //任务单编号
    NSString *jobOrderID = [self.orderBaseInfo objectFormatStringForKey:@"任务单编号"];
    NSString *station = [self.orderBaseInfo objectFormatStringForKey:@"DB"];
    
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeGroup];
    [content setModuleType:@"HTGL_FHD_LIST"];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    
    [content addParameterKey:@"任务单编号" value:jobOrderID];
    [content addParameterKey:@"DB" value:station];
    [content addParameterKey:@"起始条数" value:state];
    [content addParameterKey:@"结束条数" value:end];
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        if(isReresh) [self.tableView headerEndRefreshing];
        else [self.tableView footerEndRefreshing];
        //添加数据，刷新页面
        NSArray *firstArray = nil;
        if(result.resultArray && result.resultArray.count>0)
            firstArray = [result.resultArray firstObject];
        
        if(firstArray && firstArray.count>0){
            if(isReresh) [self.logisticsData removeAllObjects];
            [self.logisticsData addObjectsFromArray:firstArray];
            [self.tableView setTableViewNotDataDrawText:nil isShow:NO];
            [self.tableView reloadData];
            [self setupRefresh];//添加下拉
        } else if(isReresh){
            [self.tableView setTableViewNotDataDrawText:@"暂无发货单" isShow:YES];
        }
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        if(isReresh)[self errorInfoViewText:@"哦，出错了"];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        if(isReresh)[self errorInfoViewText:@"无法连接网络"];
    }];
}

- (void)errorInfoViewText:(NSString*)label
{
    [self.tableView headerEndRefreshing];
    [self.tableView footerEndRefreshing];
    
    if (self.tableView.visibleCells.count <= 0) {
        [self.tableView setTableViewNotDataDrawText:label isShow:YES];
    } else {
        [self.tableView setTableViewNotDataDrawText:nil isShow:NO];
        [self showErrorHUDText:label];
    }
}

#pragma mark UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 30)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(8, 0, SuperWidth - 10, 29.5)];
    //lable.textColor = [UIColor colorWithRed:28/255.00 green:182/255.00 blue:220/255.00 alpha:1.0];
    lable.textColor = [UIColor colorWithWhite:0.33 alpha:1.0];
    lable.font = [UIFont boldSystemFontOfSize:15];
    lable.text = [NSString stringWithFormat:@"发货单详情"];
    [view addSubview:lable];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 29.5, SuperWidth, 0.5)];
    lineView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.1];
    [view addSubview:lineView];
    
    return view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return logisticsData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MTCRWDLogisticsInfoViewCell";
    MTCRWDLogisticsInfoViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
        cell = [[MTCRWDLogisticsInfoViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
    NSDictionary *itemInfo = [logisticsData objectAtIndex:indexPath.row];
    cell.driverLabel.text = [itemInfo objectFormatStringForKey:@"司机" defaults:@"未知"];
    cell.startTimeLabel.text = [itemInfo objectFormatStringForKey:@"创建时间" defaults:@"未知"];
    cell.fhdbhLabel.text = [itemInfo objectFormatStringForKey:@"发货单编号" defaults:@"未知"];
    cell.capacityLabel.text = [itemInfo objectFormatStringForKey:@"发货方量" format:@"运量%@方" defaults:@"未知"];
    
    if (indexPath.row == 0) {
        cell.numberImageView.hidden = NO;
        cell.numberLabel.hidden = YES;
    } else {
        cell.numberImageView.hidden = YES;
        cell.numberLabel.hidden = NO;
        cell.numberLabel.text = [itemInfo objectFormatStringForKey:@"第几车"];
    }
    
    NSString *CPH = [itemInfo objectFormatStringForKey:@"车牌号" defaults:@"未知"];
    NSString *CH = [itemInfo objectFormatStringForKey:@"车辆编号" defaults:@"未知"];
    cell.licenseNumberLabel.text = [NSString stringWithFormat:@"%@(%@)",CPH,CH];
    
    return cell;
}

#pragma mark UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 82;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

@end
